<?php
class PostTermsController extends AppController {

    public $name = 'PostTerms';
    public $paginate = array(
        'order' => 'order ASC'
    );

    public function index(){
        $this->set('postTerms', $this->paginate());
    }

    public function view($id = null){
        if(!$id){
            $this->Session->setFlash(__('Invalid PostTerm.', true));
            $this->redirect(array('action'=>'index'));
        }
        $this->Response->ajax();
        $this->PostTerm->recursive = 1;
        $this->set('postTerm', $this->PostTerm->read(null, $id));
    }

    public function add(){
        if(!empty($this->data)){
            $this->PostTerm->create();
            $data = $this->PostTerm->save($this->data);
            if($data){
                $this->Session->setFlash(__('The PostTerm has been saved', true));
            }else{
                $this->Session->setFlash(__('The PostTerm could not be saved. Please, try again.', true));
            }
        }else{
            $this->Session->setFlash(__('Invalid Add Request', true));
        }
        $this->redirect(array('action'=>'index'));
    }

    public function edit($id = null){
        if(!$id && empty($this->data)){
            $this->Session->setFlash(__('Invalid PostTerm', true));
            $this->redirect(array('action'=>'index'));
        }
        if(!empty($this->data)){
            if($this->PostTerm->save($this->data)){
                $this->Session->setFlash(__('The PostTerm has been saved', true));
            }else{
                $this->Session->setFlash(__('The PostTerm could not be saved. Please, try again.', true));
            }
            $this->redirect(array('action'=>'index'));
        }
        if(empty($this->data)){
            $this->Response->ajax();
            $this->data = $this->PostTerm->read(null, $id);
        }
    }

    public function check(){
        if(!empty($this->data)){
            $term = $this->PostTerm->findByName($this->data['PostTerm']['name']);
            if(empty($term['PostTerm']['name'])){
                $this->Response->json(true, 'PostTerm name check success', $term, null);
            }else{
                $this->Response->json(false, 'PostTerm name check failure', $term, null);
            }
        }else{
            $this->Response->json(false, 'Invalid PostTerm name check request', null, null);
        }
    }

    /**
     * Saving PostTerm display orders, which happens when sorting in the client side
     *
     * @access: public
     * @param: none
     */
    public function order(){
        $this->Response->ajax();
        if(empty($this->data)){
            $this->Response->json(false, __('No sort data provided', true));
        }else{
            if($this->PostTerm->save($this->data)){
                $this->Response->json(true, __('New order save success', true));
            }else{
                $this->Response->json(false, __('New order save failed', true));
            }
        }
    }

}
?>